layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/member/JsonList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {field: 'cardNo', title: 'ID',align:'center',width:160},
            {field: 'nickName', title: '微信昵称',align:'center',width:160},
            {field: 'phone', title: '手机',align:'center',width:100},
            {field: 'gradeName', title: '级别',align:'center',width:100},
            {field: 'headImage', title: '头像',align:'center', templet:function(d){
                return "<img src='"+d.headImage+"' style='width: 80px;height: 80px;'/>";
            },width:100},
            {field: 'money', title: '剩余金额',align:'center',width:120, templet:function(d){
                return d.money+"元";
            }},
            {field: 'oneUserId', title: '间接介绍人',align:'center'},
            {field: 'introductUserId', title: '直接介绍人',align:'center'},
            {field: 'createTime', title: '创建时间', align:'center',width:180, templet:function(d){
                return d.createtime;
            }},
            {title: '操作',align:"center", width:140,templet:function(d){
                return '<a class="layui-btn layui-btn-xs" lay-event="address">地址</a>'+
                    '<a class="layui-btn layui-btn-xs" lay-event="gradeEdit">级别</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                id: $("#id").val(),
                nickName: $("#nickName").val(),
                grade: $("#grade").val(),
                cardNo: $("#cardNo").val(),
                areaUserId: $("#areaUserId").val(),
                oneUserId: $("#oneUserId").val(),
                introductUserId: $("#introductUserId").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/member/SelectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        } else if(layEvent === 'address'){ //地址信息
            window.location.href="/address/listPage?memberId="+data.id;
        } else if(layEvent === 'gradeEdit'){//收货员设置
            // 弹窗
            var index = layui.layer.open({
                title :"级别设置",
                type : 2,
                content : "/member/memberGradeSet?memberId="+data.id+"&grade="+data.grade,
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                    body.find(".closeLay").bind("click",function () {
                        layui.layer.close(index);
                    })
                    //layui.layer.full(index);
                }
            });
        }
    });

})